<?php
declare (strict_types = 1);

namespace app\command;

use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use app\model\JdHistory;
use app\model\JdUser;
use app\model\JdBeanRecord;
use Spyc;

class UpdateCookie extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('app\command\updatecookie')
            ->setDescription('the app\command\updatecookie command')
            ->addArgument('dir', Argument::OPTIONAL, "root dir of docker-compose.yml")
            ;
    }

    protected function execute(Input $input, Output $output)
    {
        $dir = $input->getArgument('dir');
        if(!$dir) {
            $output->writeln("请输入dir参数");
            return;
        }
        $this->listFolderFiles($dir, $output);
    }

    private function listFolderFiles($dir, Output $output){
        // $output->writeln("listFolderFiles:".$dir);
        $ffs = scandir($dir);
    
        unset($ffs[array_search('.', $ffs, true)]);
        unset($ffs[array_search('..', $ffs, true)]);
        if (count($ffs) < 1)
            return;

        foreach($ffs as $ff){
            if(is_dir($dir.DIRECTORY_SEPARATOR .$ff)){
                $this->listFolderFiles($dir.DIRECTORY_SEPARATOR.$ff, $output);
            } else {
                if($ff == 'docker-compose.yml'){
                    $output->writeln("parse file:".$dir.DIRECTORY_SEPARATOR .$ff);
                    $this->parseFile($dir.DIRECTORY_SEPARATOR .$ff, $output);
                }
            }
        }
    }

    private function parseFile($file, Output $output){
        $config = Spyc::YAMLLoad($file);
        // var_dump($config);
        if(!$config || !isset($config['jd_scripts']) || !isset($config['jd_scripts']['environment'])){
            $output->writeln("文件没有配置cookie");
            return;
        }
        $envs = $config['jd_scripts']['environment'];
        foreach ($envs as &$env) {
            if( strpos($env, 'JD_COOKIE=') === 0){
                
                $cookiestr = substr($env, 10);
                
                if($cookiestr){
                    $cookies = explode('&', $cookiestr);
                    // var_dump($cookies);
                    foreach ($cookies as $cookie){
                        if($cookie){
                            $values = explode('; ', $cookie);
                            //var_dump($values);
                            if(count($values) == 2){
                                $ptpin = urldecode($values[1]);
                                $ptpin = substr($ptpin, 7);
                                $ptpin = trim($ptpin, ";");
                                $user = JdUser::where('pt_pin', $ptpin)->find();
                                if($user) {
                                    $user->cookie = $cookie;
                                    $user->save();
                                    $output->writeln("更新用户{$ptpin} cookie");
                                } else {
                                    $user = new JdUser();
                                    $user->cookie = $cookie;
                                    $user->pt_pin = $ptpin;
                                    $user->save();
                                    $output->writeln("新增用户{$ptpin} cookie");
                                }
                            }
                        }
                    }
                }
                break;
            }
        }
    }
    
}
